persistence.setNamespace("peiwan_weixin");
var peiwanWeixin = angular.module('peiwan_weixin', []);
peiwanWeixin.controller('RegisterCtrl2', [
		"$scope",
		function(s) {
			s.countStr = "获取验证码";
			(function() {
				var headUrl = persistence.get("headUrl");
				var nickname = persistence.get("nickname");
				var gender = persistence.get("gender");
				var birth = persistence.get("birth");
				if (headUrl == null || nickname == null || gender == null
						|| birth == null) {
					alert("请先填写完成资料");
					location.href = "register.html";
				}
			})();
			var code;
			s.sendCode = function() {
				var key = "com.cj.peiwan";
				var phone = $("#username").val();
				if (phone == "") {
					swal({
						title : "",
						text : "请填写手机号码",
						type : "error"
					});
					return;
				}
				var timer = new CountDownTimer(60000, 1000);
				timer.onTick = function(count) {
					s.$apply(function() {
						s.countStr = "已发送(" + count / 1000 + ")";
					});
				}
				timer.onStop = function() {
					s.$apply(function() {
						s.countStr = "获取验证码";
					});
					$("#sendCodeBtn").attr("disabled", false);
					$("#sendCodeBtn").css("background", "rgb(94, 185, 94)");
				}
				timer.start();
				$("#sendCodeBtn").attr("disabled", true);
				$("#sendCodeBtn").css("background", "grey");
				var sign = SparkMD5.hash(key + phone);
				ajaxWeixin("users/sendCode", {
					phone : phone,
					sign : sign
				}, function(data) {
					var json = JSON.parse(data);
					if (json.status == 1) {
						swal({
							title : "",
							text : "发送短信失败",
							type : "error"
						});
					} else if (json.status == 2) {
						swal({
							title : "",
							text : "用户已经存在",
							type : "error"
						});
					} else if (json.status == 0) {
						swal({
							title : "",
							text : "发送短信成功",
							type : "success"
						});
						code = json.entity;
					}
				});
			};
			s.ok = function() {
				var headUrl = persistence.get("headUrl");
				var nickname = persistence.get("nickname");
				var gender = persistence.get("gender");
				var birth = persistence.get("birth");
				var year = birth.split("/")[0];
				var month = birth.split("/")[1];
				var day = birth.split("/")[2];
				var username = $("#username").val();
				var veryCode = $("#veryCode").val();
				var password = $("#password").val();
				if (username == "") {
					swal({
						title : "",
						text : "请填写手机号码",
						type : "error"
					});
					return;
				}
				if (veryCode == "") {
					swal({
						title : "",
						text : "请填写验证码",
						type : "error"
					});
					return;
				}
				if (password == "") {
					swal({
						title : "",
						text : "请填写密码",
						type : "error"
					});
					return;
				}
				password = SparkMD5.hash(password);
				$("#gender_modal").show();
				ajaxWeixin("users/register", {
					username : username,
					password : password,
					verifyCode : veryCode,
					nickname : nickname,
					gender : gender,
					year : year,
					month : month,
					day : day,
					headUrl : headUrl
				}, function(data) {
					$("#gender_modal").css("display", "none");
					var json = JSON.parse(data);
					if (json.status == 0) {
						swal({
							title : "",
							text : "注册成功",
							type : "success"
						});
						persistence.put("session", json.extra);
						location.href = "peiwan_form.html"
					} else if (json.status == 1) {
						swal({
							title : "",
							text : "验证码错误",
							type : "error"
						});
					} else if (json.status == 2) {
						swal({
							title : "",
							text : "重复的用户",
							type : "error"
						});
					}
				});
			};
		} ]);
